<?php

//UMA new
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Company extends Model
{
    protected $fillable = [
        'company_name',
        'company_id',
        'api_authentication_key',
        'status',
    ];

    /**
     * boot
     * 自动设置 api_authentication_key 的值
     * @return void
     */
    public static function boot()
    {
        parent::boot();
        self::creating(function ($model) {
            $model->api_authentication_key = Str::uuid("version 4");
        });
    }
    
    /**
     * one company has many customers
     *
     * @return hasMany
     */
    public function customers()
    {
        return $this->hasMany(Customer::class);
    }

    /**
     * one to many relation
     * @return HasMany
     */
    public function customer_histories()
    {
        return $this->hasMany(CustomerHistory::class, 'company_id');
    }

    /**
     * @param $query
     * @param $company_id
     * @return mixed
     */
    public function scopeCompanyId($query, $company_id)
    {
        return $query->where('company_id', $company_id);
    }

    /**
     * @param $query
     * @param $status
     * @return mixed
     */
    public function scopeByStatus($query, $status)
    {
        return $query->where('status', $status);
    }

    public function scopeActive($query) {
        return $query->where('status', 1);
    }
}
